<?php
namespace app\admin\controller;

use think\Db;
use think\Session;
use think\Validate;

class Admin extends Main
{
    /**
     * 管理员列表
    */
    public function index(){
        $where = [];
        $keyword = $this->request->param('keyword');

        if($keyword){
            $where[] = ['username','like','%'.$keyword.'%'];
        }

        $data = Db::name('admin')
            ->alias('a')
            ->where($where)
            ->order('id desc')
            ->paginate(15, false, [
                'query' => request()->param()]);
        $this->assign('list', $data);
        return $this->fetch();
    }

    //增加管理员
    public function addAdmin()
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $request->post();

            $validate = validate('Admin');
            $res      = $validate->scene('add')->check($post);
            if ($res !== true) {
                $this->error($validate->getError());
            } else {
                if($post['check_password'] != $post['password']){
                    $this->error('两次密码不一致');
                }
                unset($post['check_password']);
                $post['password'] = md5($post['password']);
                $post['create_time']   = date('Y-m-d h:i:s', time());
                $db = Db::name('admin')
                    ->insert($post);

                $this->success('添加成功');
            }
        }else{
            return $this->fetch('add');
        }

    }

    //编辑提交
    public function editAdmin($id)
    {
        $request = $this->request;
        if($request->isPost()){
            $post     = $this->request->post();
            if($post['id']==1 ){
                if(session('admin_id')!==1)  $this->error('系统管理员无法修改');
            }

            $validate = validate('Admin');
            $res = $validate->scene('edit')->check($post);
            if ($res !== true) {
                $this->error($validate->getError());
            }

            if ($post['password']) {
                if($post['check_password'] != $post['password']){
                    $this->error('两次密码不一致');
                }
                $post['password'] = md5($post['password']);
            }else{
                unset($post['password']);
            }
            unset($post['check_password']);
            $db = Db::name('admin')
                ->where('id', $post['id'])
                ->update($post);
			session('username', $post['username']);
            $this->success('编辑成功');
        }else{
            $data = Db::name('Admin')
                ->where('id', $id)
                ->find();
            $this->assign('data', $data);
            return $this->fetch('edit');
        }

    }

    //删除用户
    /*public function deleteAdmin()
    {
        $id = $this->request->post('id');
        $username =  Db::name('admin')
            ->where('id',$id)
            ->value('username');
        if ((int) $id !== 1) {
            if($username!==session('username')){
                 $db = Db::name('admin')
                ->where('id', $id)
                ->setField('status');
                $this->success('删除成功');
            }else{
                 $this->error('无法删除当前登录用户');
            }
        } else {
            $this->error('超级管理员无法删除');
        }
    }*/

}
